package com.beone.admin.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.beone.admin.entity.ImportDetail;
import com.beone.admin.service.ImportDetailService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.base.SuperServiceImpl;
import com.beone.admin.entity.TaskTemplate;
import com.beone.admin.entity.TaskTemplateDetail;
import com.beone.admin.helper.TaskTemplateHelper;
import com.beone.admin.mapper.TaskTemplateMapper;
import com.beone.admin.service.TaskTemplateDetailService;
import com.beone.admin.service.TaskTemplateService;
import com.beone.admin.utils.PaginationGatagridTable;
import com.beone.admin.utils.ServiceUtils;

/**
 * @Title 排班模板 服务实现类
 * @Author 秦波
 * @Version 1.0 on 2019-06-01
 * @Copyright 贝旺科权
 */
@Service
public class TaskTemplateServiceImpl extends SuperServiceImpl<TaskTemplateMapper, TaskTemplate>
    implements TaskTemplateService {

    @Autowired
    private TaskTemplateDetailService taskTemplateDetailService;
    @Autowired
    private ImportDetailService importDetailService;

    /**
     * 分页显示排班模板列表
     * 
     * @param entity
     * @param currPage
     *            当前页码
     * @param pageSize
     *            每页显示记录数
     * @return
     */
    @Override
    public PaginationGatagridTable getTaskTemplatePagination(TaskTemplate entity, int currPage, int pageSize) {
        EntityWrapper<TaskTemplate> ew = new EntityWrapper<>();
        Page<TaskTemplate> page = new Page<>(currPage, pageSize);
        List<TaskTemplate> rows = baseMapper.selectPage(page, ew);
        return ServiceUtils.createGatagridTableJson(page, rows);
    }

    @Override
    public XSSFWorkbook getExcelForTemplate(Integer deptId, String month) {
        TaskTemplate taskTemplate = getTaskTemplate(deptId);
        if (null == taskTemplate) {
            throw new RuntimeException("未找到模板");
        }
        try {
            Date date = new SimpleDateFormat("yyyy-MM").parse(month);
            // 设置当前月的第一天
            Calendar cale = Calendar.getInstance();
            cale.setTime(date);
            cale.add(Calendar.MONTH, 0);
            cale.set(Calendar.DAY_OF_MONTH, 1);
            taskTemplate.setBeginDate(cale.getTime());
            // 设置当前月的最后一天
            cale = Calendar.getInstance();
            cale.add(Calendar.MONTH, 1);
            cale.set(Calendar.DAY_OF_MONTH, 0);
            taskTemplate.setEndDate(cale.getTime());
        } catch (ParseException e) {
            System.err.println("时间转换异常");
        }

        List<TaskTemplateDetail> detailList = taskTemplateDetailService.getDetailListByTemplatId(taskTemplate.getId());
        return TaskTemplateHelper.getWb(taskTemplate, detailList, null);
    }

    @Override
    public XSSFWorkbook getExcelWithData(Integer deptId, Date startDate, Date endDate) {
        TaskTemplate taskTemplate = getTaskTemplate(deptId);
        if (null == taskTemplate) {
            throw new RuntimeException("未找到模板");
        }
        List<TaskTemplateDetail> detailList = taskTemplateDetailService.getDetailListByTemplatId(taskTemplate.getId());
        List<ImportDetail> dataList = importDetailService.getImportDetailList(deptId, startDate, endDate);
        taskTemplate.setBeginDate(startDate);
        taskTemplate.setEndDate(endDate);
        return TaskTemplateHelper.getWb(taskTemplate, detailList, dataList);
    }

    @Override
    public TaskTemplate getTaskTemplate(Integer deptId) {
        EntityWrapper<TaskTemplate> ew = new EntityWrapper<>();
        ew.eq("dept_id", deptId);
        List<TaskTemplate> taskTemplates = baseMapper.selectList(ew);
        if (CollectionUtils.isNotEmpty(taskTemplates)) {
            return taskTemplates.get(0);
        }
        return null;
    }
}
